//
//  AppDelegate.m
//  SeaChat
//
//  Created by 潘强 on 16/6/13.
//  Copyright © 2016年 潘强. All rights reserved.
//

#import "AppDelegate.h"
#import "TalkViewController.h"
#import "AddressViewController.h"
#import "FriendViewController.h"
#import "MineViewController.h"
#import "Login&RegViewController.h"
#import <AMapFoundationKit/AMapFoundationKit.h>

@interface AppDelegate ()<EMClientDelegate,EMChatManagerDelegate,EMContactManagerDelegate,UITabBarControllerDelegate>
@property(nonatomic,assign)NSInteger num;
@property(nonatomic,retain)UINavigationController *ADDNA;



@property(nonatomic,retain)UITabBarController *tabbar;
@end

@implementation AppDelegate
-(void)dealloc{
    [_window release];
    [_ADDNA release];
     //移除环信代理
    [[EMClient sharedClient] removeDelegate:self];
    [[EMClient sharedClient].contactManager removeDelegate:self];
    [[EMClient sharedClient ].chatManager  removeDelegate:self];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 1.注册环信SDK

    NSLog(@"%@",NSHomeDirectory());

    EMOptions *options = [EMOptions optionsWithAppkey:@"lpl-team#seachat"];
    options.apnsCertName = @"PushCert";
    [[EMClient sharedClient] initializeSDKWithOptions:options];
    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    //写个nil 默认代理会在主线程调用
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];


//配置地图apikey
    [AMapServices sharedServices].apiKey = @"2ad06158a1e8522af8f72cb3d3a70230";

    
    //TalkViewController 页面
    TalkViewController *TalkViewC = [[TalkViewController alloc]init];
    
    UINavigationController *TalkNC = [[UINavigationController alloc]initWithRootViewController:TalkViewC];
    
    //加tabBar样式
    TalkNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"会话" image:[UIImage imageNamed:@"huihua"] tag:10001];
    
   
    
    //AddressViewController 页面
    AddressViewController *AddVC = [[AddressViewController alloc]init];
    
    UINavigationController *AddNC = [[UINavigationController alloc]initWithRootViewController:AddVC];
    
    AddNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"通讯录" image:[UIImage imageNamed:@"tongxunlu"] tag:10002];
    
    
    //FriendViewController页面

    FriendViewController *FriendVC = [[FriendViewController alloc]init];
    
    UINavigationController *FriendNC = [[UINavigationController alloc]initWithRootViewController:FriendVC];
    
    FriendNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"周边" image:[UIImage imageNamed:@"pengyouquan"] tag:10003];
    
    
    
    //MineViewController页面
    MineViewController *MineVC = [[MineViewController alloc]init];
    
    UINavigationController *MineNC = [[UINavigationController alloc]initWithRootViewController:MineVC];
    
    MineNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"wode"] tag:10004];
    

    //创建tabBar
   __block UITabBarController *tabel = [[UITabBarController alloc]init];


    tabel.viewControllers = @[TalkNC,AddNC,FriendNC,MineNC];


    Login_RegViewController *LRVC = [[Login_RegViewController alloc]init];
#pragma mark FMDB
    [[DataBaseManeger sharaInstance] openDB];
    [[DataBaseManeger sharaInstance] createTable];
    

    LRVC.tabbar = tabel;
    
    if ([EMClient sharedClient].isLoggedIn) {
        self.window.rootViewController = tabel;
    }else{
        self.window.rootViewController = LRVC;

    }
    __block AppDelegate *root = self;
    LRVC.backToMainVC = ^(){

        root.window.rootViewController = tabel;

    };
    tabel.delegate = self;
    self.num = 0;
    self.ADDNA = AddNC;
    [TalkViewC release];
    [TalkNC release];
    [AddVC release];
    [AddNC release];
    [FriendVC release];
    [FriendNC release];
    [MineVC release];
    [MineNC release];
    
 
    //aaaaaaa
    

        return YES;
}
- (void)didAutoLoginWithError:(EMError *)aError{
       
}

- (void)didReceiveFriendInvitationFromUsername:(NSString *)aUsername
                                       message:(NSString *)aMessage{

    self.num ++;
    if (self.num != 0) {
        _ADDNA.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld",self.num];
        NSLog(@"22");
}


}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    NSLog(@"%ld",tabBarController.selectedIndex);
    viewController.tabBarItem.badgeValue = nil;

    self.num = 0;

}


- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [[EMClient sharedClient] applicationDidEnterBackground:application];




    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

    [[EMClient sharedClient] applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
